package com.lyeducation.repositoryImpl.system;

import com.lyeducation.entities.system.SysRoleEntity;
import com.lyeducation.repository.system.SysRoleRepository;
import com.lyeducation.repositoryImpl.base.BaseDateRepositoryImpl;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

@Repository
public class SysRoleRepositoryImpl extends BaseDateRepositoryImpl<SysRoleEntity>
    implements SysRoleRepository {
  @Override
  public void findTreeRole(SysRoleEntity sysRoleEntity) {
    String sql = "SELECT r.* from sys_role r where r.parentId=:parentId";
    Map<String, Object> map = new HashMap(1);
    map.put("parentId", sysRoleEntity.getId());
    List<SysRoleEntity> sysRoleEntities = findByNativeQuery(sql, map);
    TreeSet<SysRoleEntity> sysRoleEntities1 = new TreeSet<>();
   for(SysRoleEntity sysRoleEntity1 :sysRoleEntities){
     sysRoleEntities1.add(sysRoleEntity1);
   }
    sysRoleEntity.setChildren(sysRoleEntities1);
    for (SysRoleEntity sysRoleEntity1 : sysRoleEntities) {
      findTreeRole(sysRoleEntity1);
    }
  }

  @Override
  public List<SysRoleEntity> findRolesByRoleIdList(Map<String, Object> paramMap) {
    String sql = null;
    for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
      sql = "SELECT r.* from sys_role r where r.id in (:" + entry.getKey() + ")";
      break;
    }
    return findByNativeQuery(sql, paramMap);
  }

  @Override
  public List<SysRoleEntity> findRolesByRoleNameList(Map<String, Object> paramMap) {
    String sql = null;
    for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
      sql = "SELECT r.* from sys_role r where r.name in (:" + entry.getKey() + ")";
      break;
    }
    return findByNativeQuery(sql, paramMap);
  }
}
